/*
 * Copyright (c) 2022 Kirky
 * Licensed under the Open Software Licence, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/osl-3.0.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lingeringdream.luciole.helper.list;

import com.lingeringdream.luciole.ResourceTable;
import com.lingeringdream.luciole.helper.ResourceHelper;
import com.lingeringdream.luciole.helper.generalDialog.GeneralDialogHelper;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.*;

/**
 * 通用的列表提供者
 *
 * @author Kirky
 * @version 1.0
 * @date 2022/5/4
 */
public class NormalListProvider extends BaseItemProvider {
    private final Context context;
    private final List<String> titles;
    private final List<String> contents;
    private final List<ClickedListener> clickedListeners;
    private final Map<Integer, Color> contentColors;
    private Color titleColor;
    private Color contentColor;
    private boolean onlyContent = false;
    private boolean arrow = false;
    private boolean line = false;

    public interface ClickedListener {
        /**
         * Normal click event
         *
         * @param component 当前组件
         */
        void normalListener(Component component);
    }


    public NormalListProvider(Context context) {
        this.context = context;
        titles = new ArrayList<>(0);
        contents = new ArrayList<>(0);
        clickedListeners = new ArrayList<>(0);
        contentColors = new HashMap<>(0);
    }

    /**
     * 设置item是否有向右的箭头
     * 只有存在向右的箭头表示，该item才可点击
     */
    public NormalListProvider setArrow(boolean arrow) {
        this.arrow = arrow;
        return this;
    }

    /**
     * 设置item是否有分割线
     */
    public NormalListProvider setLine(boolean line) {
        this.line = line;
        return this;
    }

    /**
     * 设置只展示content内容
     */
    public NormalListProvider setOnlyContent(boolean onlyContent) {
        this.onlyContent = onlyContent;
        return this;
    }

    /**
     * 设置通用title颜色
     */
    public NormalListProvider setTitleColor(Color titleColor) {
        if (titleColor != null) {
            this.titleColor = titleColor;
        }
        return this;
    }

    /**
     * 设置通用content颜色
     */
    public NormalListProvider setContentColor(Color contentColor) {
        if (contentColor != null) {
            this.contentColor = contentColor;
        }
        return this;
    }

    public NormalListProvider addItem(String title, String content, ClickedListener listener) {
        if (title != null && !"".equals(title) && content != null && !"".equals(content)) {
            titles.add(title);
            contents.add(content);
        }
        if (listener != null) {
            clickedListeners.add(listener);
        }
        return this;
    }

    public NormalListProvider addItem(String title, String content) {
        return addItem(title, content, null);
    }

    public NormalListProvider addItem(String title, Color contentColor, String content, ClickedListener listener) {
        addItem(title, content, listener);
        if (contentColor != null) {
            contentColors.put(titles.size(), contentColor);
        }
        return this;
    }

    public NormalListProvider addItem(String title, Color contentColor, String content) {
        return addItem(title, contentColor, content, null);
    }

    public NormalListProvider addItemOnlyContent(String content, ClickedListener listener) {
        if (content != null && !"".equals(content)) {
            contents.add(content);
        }
        if (listener != null) {
            clickedListeners.add(listener);
        }
        return this;
    }

    public NormalListProvider addItemOnlyContent(String content) {
        return addItem(content, null);
    }

    public NormalListProvider clear() {
        if (titles.size() > 0) {
            titles.clear();
        }
        if (contents.size() > 0) {
            contents.clear();
        }
        if (clickedListeners.size() > 0) {
            clickedListeners.clear();
        }
        if (contentColors.size() > 0) {
            contentColors.clear();
        }
        return this;
    }

    @Override
    public int getCount() {
        if (titles.size() == contents.size()) {
            return titles.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (position >= 0 && position < getCount()) {
            String[] item = new String[2];
            item[0] = titles.get(position);
            item[1] = contents.get(position);
            return item;
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        if (position >= 0 && position < getCount()) {
            return position;
        }
        return 0L;
    }

    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        final Component component;
        if (convertComponent == null) {
            component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_layout_detail_item, null, false);
        } else {
            component = convertComponent;
        }
        if (line) {
            component.findComponentById(ResourceTable.Id_detail_item_line).setVisibility(Component.VISIBLE);
        }
        if (arrow) {
//            箭头可见
            Image arrow = component.findComponentById(ResourceTable.Id_detail_item_arrow);
            arrow.setVisibility(Component.VISIBLE);
//            中间内容空出箭头的位置
            DirectionalLayout directionalLayout = component.findComponentById(ResourceTable.Id_detail_item_layout);
            directionalLayout.setMarginRight(arrow.getWidth() * 2);
//            设置点击事件
            component.setClickedListener(component1 -> clickedListeners.get(position).normalListener(component));
        }

        Text textTitle = component.findComponentById(ResourceTable.Id_detail_item_title);
        if (onlyContent) {
            textTitle.setVisibility(Component.HIDE);
        } else {
            if (titleColor != null) {
                textTitle.setTextColor(titleColor);
            }
            textTitle.setText(titles.get(position));
        }

        Text textContent = component.findComponentById(ResourceTable.Id_detail_item_content);
        Color contentColor = contentColors.get(position);
        if (contentColor != null) {
            textContent.setTextColor(contentColor);
        } else if (this.contentColor != null) {
            textContent.setTextColor(this.contentColor);
        }
        textContent.setText(contents.get(position));

        return component;
    }
}
